<?php
/**
 * @author Mingming Tang iamalazyrat@gmail.com
 */

class Search_Controller extends Base_Controller {

  public $restful = true;

  public function get_index() {
    $page = Input::get('p', 1);
    $per_page = 40;
    $keyword = Input::get('keyword', false);
    if (!$keyword) return Redirect('/');
    $category = Category::all();
    $cids = array(0);
    foreach ($category as $c) $cids[] = $c->cid;
    if ($cids OR $keyword) {
      $field = array('num_iid', 'seller_id', 'nick', 'title', 'price', 'item_location', 'seller_credit_score', 'click_url', 'shop_click_url', 'pic_url', 'taobaoke_cat_click_url', 'keyword_click_url', 'coupon_rate', 'coupon_price', 'coupon_start_time', 'coupon_end_time', 'commission_rate', 'commission', 'commission_num', 'commission_volume', 'volume', 'shop_type');
      $c = new TopClient;
      $c->appkey = Config::get('option.appkey', '');
      $c->secretKey = Config::get('option.secret', '');
      $req = new TaobaokeItemsGetRequest;
      $req->setFields(implode(',', $field));
      $req->setCid(30);
      $req->setSort('commissionNum_desc');
      $req->setKeyword($keyword);
      $req->setPageNo($page);
      $resp = $c->execute($req);
      $products = $resp->taobaoke_items;
    } 
    $cnt = 400;
    $url = url('search') . "?keyword={$keyword}";
    $page = Pagination::page($page, $cnt, 40, $url);
    return View::make('search.index.main')->with('page', $page)->with('products', $products)->with('keyword', $keyword);
  }
}
